import Vue from 'vue'
import Vuex from 'vuex'
import {commonLoginApi,getShareCodeApi,getUserInfoApi, codeLoginApi, miniWxLoginApi, miniAuthLoginApi} from '@/api/user.js'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		hasLogin: false, // 是否登录
		open_id: '',
		token: '', // 登录凭证
		user_id: '', // 用户id
		userInfo: {}, // 用户信息
		currentTabbarIndex: 0 ,// 当前tabbar
		tabbar: [], // tabbar
		shareCode: {}, // 分享和二维码
	},
	mutations: {
		SET_TABBAR_INDEX(state,data) {
			state.currentTabbarIndex = data
		},
		SET_TABBAR(state,data) {
			state.tabbar = data
		},
		// 登录信息
		SET_LOGIN(state,{openId = '',sess_id = '', user_id = ''}) {
			state.open_id = openId
			state.token = sess_id
			state.user_id = user_id
			const hasLogin = sess_id ? true : false
			state.hasLogin = hasLogin
			// 存本地
			uni.setStorageSync('open_id',openId)
			uni.setStorageSync('token',sess_id)
			uni.setStorageSync('user_id',user_id)
			uni.setStorageSync('hasLogin',hasLogin)
		},
		// 用户信息
		SET_USERINFO(state,data) {
			state.userInfo = data
		},
		// 分享和二维码
		SET_SHARECODE(state,data) {
			state.shareCode = data
		}
	},
	actions: {
		// 账号密码登录
		userNameLogin({commit,dispatch},{username = '', password = ''}) {
			return new Promise(async (resolve,reject) => {
				// 账号密码登录
				const res = await commonLoginApi({
					username,
					password
				})
				if(!res.data.is_login) {
					uni.showToast({
						title: '用户名/手机号或密码错误',
						icon: 'none'
					})
					return
				}
				const {user_id,sess_id} = res.data
				commit('SET_LOGIN',res.data)
				dispatch('getShareCodeData')
				dispatch('getUserInfoData')
				uni.showToast({
					title: '登录成功!'
				})
				setTimeout(()=> {
					uni.navigateBack()
				},300)
			})
		},
		// 验证码登录
		codeLogin({commit,dispatch},{mobile = '', code = ''}) {
			return new Promise(async (resolve,reject) => {
				// 账号密码登录
				const res = await codeLoginApi({
					mobile: mobile,
					verCode: code
				})
				if(!res.data.is_login) {
					uni.showToast({
						title: '验证码错误',
						icon: 'none'
					})
					return
				}
				const {user_id,sess_id} = res.data
				commit('SET_LOGIN',res.data)
				dispatch('getShareCodeData')
				dispatch('getUserInfoData')
				uni.showToast({
					title: '登录成功!'
				})
				setTimeout(()=> {
					uni.navigateBack()
				},300)
			})
		},
		// 微信code 登录
		wxCodeAuth() {
			return new Promise((resolve,reject) => {
				uni.login({
					provider:'weixin',
					success: async e => {
						const res = await miniWxLoginApi({
							code: e.code,
							platform: 'MP-WEIXIN'
						})
						resolve(res.data)
					}
				})
			})
		},
		// 小程序登录并获取手机号
		miniWxLogin({
			dispatch,
			commit,
			state
		},data) {
			return new Promise(async (resolve,reject) => {
				const res = await miniAuthLoginApi(data)
				commit('SET_LOGIN',res.data)
				dispatch('getShareCodeData')
				dispatch('getUserInfoData')
				uni.showToast({
					title: '登录成功!'
				})
				setTimeout(()=> {
					uni.navigateBack()
				},300)
				resolve(res)
			})
		},
		// 获取分享和二维码
		async getShareCodeData({state,commit}) {
			const res = await getShareCodeApi({
				user_id: state.user_id,
				token: state.token
			})
			commit('SET_SHARECODE',res.data)
		},
		// 获取用户信息
		async getUserInfoData({state,commit}) {
			const res = await getUserInfoApi({
				user_id: state.user_id,
				token: state.token
			})
			commit('SET_USERINFO',res.data)
		}
	}
})

export default store